package com.xpec.c4.service.contexts;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * A basic implementation of Context that keeps the state in a Map.
 * 
 * @author Bany
 */
public class BasicContext implements Context {

	protected Map<String, Object> map;
	protected ScopeType scope;

	public BasicContext(ScopeType scope) {
		this.scope = scope;
		this.map = new HashMap<String, Object>();
	}

	protected BasicContext(ScopeType scope, Map<String, Object> map) {
		this.scope = scope;
		this.map = map;
	}

	public ScopeType getType() {
		return scope;
	}

	public Object get(String name) {
		return map.get(name);
	}

	public String[] getNames() {
		// yes, I know about the toArray() method,
		// but there is a bug in the RI!
		Set<String> keySet = map.keySet();
		String[] array = new String[keySet.size()];
		int i = 0;
		for (String key : keySet) {
			array[i++] = key;
		}
		return array;
	}

	public boolean isSet(String name) {
		return map.containsKey(name);
	}

	public void remove(String name) {
		map.remove(name);
	}

	public void set(String name, Object value) {
		map.put(name, value);
	}

	public void flush() {
	}

	@Override
	public String toString() {
		return "BasicContext(" + scope + ")";
	}

	@Override
	public void clear() {
		map.clear();

	}

}
